package asxpgyp.okhttputil.com.retrofitdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    //声明日志类
    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
    //自定义OkHttpClient
    OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    //设定日志级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//添加拦截器
        okHttpClient.addInterceptor(httpLoggingInterceptor);
        request();
    }

    public void request() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(" http://120.76.205.241:8000/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient.build())
                .build();
        AppURL url = retrofit.create(AppURL.class);
        Call<ResponseBody> call = url.getIndex("wzCafoX4TEWwZY4SNmdprUbEvOVrihj62MTdOEb4ZvBtWfkUmsGOPesBM7EAKUiO");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.d("MainActivity", response.toString());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("tag", t.getMessage());//请求失败
            }
        });
    }
}
